package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.JsonObject.SelfMessage;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/5/6.
 */

public class MySelf2Adapter extends RecyclerView.Adapter {
    private Context context;
    private List<SelfMessage.ListBean> list;
    private LayoutInflater layoutInflater;
    private int bottom_count,tag;
    private final int LEFT=10,RIGHT=20;
    public MySelf2Adapter(Context context, List<SelfMessage.ListBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater=LayoutInflater.from(context);
    }

    public void UpData(List<SelfMessage.ListBean> list,int bottom_count,int tag){
        this.list=list;
        this.bottom_count=bottom_count;
        this.tag=tag;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view=layoutInflater.inflate(R.layout.myself_item_2,viewGroup,false);
        View view2=layoutInflater.inflate(R.layout.myself_item_2_y,viewGroup,false);
        View view_bottom=layoutInflater.inflate(R.layout.bottom_view,viewGroup,false);
        AutoUtils.autoSize(view);
        AutoUtils.autoSize(view2);
        AutoUtils.autoSize(view_bottom);

        if(i==ManagerItem.Bottom){
            return new BottomHolder(view_bottom);
        }else if(i==LEFT){
            return new SelfItem2Holder(view);
        }else if(i==RIGHT){
            return new SelfItem2Holder(view2);
        }else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof SelfItem2Holder){
            ((SelfItem2Holder) holder).position=position;
            if(list!=null&&list.size()!=0){
                ((SelfItem2Holder) holder).myself_item2_time.setText(list.get(position).getCreateTime());
                ((SelfItem2Holder) holder).myself_item_2_tv.setText(list.get(position).getMessageContent());
                Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE+1,new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID",MyInfo.User_Id+"").build()))
                        .transform(new GlideCircleTransform(context))
                        .error(R.mipmap.default_head).into(((SelfItem2Holder) holder).myself_item_2_iv);
            }
        }else if(holder instanceof BottomHolder){
            if(bottom_count==1&&tag==0){
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            }else if(bottom_count==1&&tag==1){
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            }else if(bottom_count==1&&tag==2){
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(list!=null&&position==list.size()){
            return ManagerItem.Bottom;
        }else if(list!=null&&position<list.size()&&list.get(position).getSenderId()== MyInfo.User_Id){
            return RIGHT;
        }else if(list!=null&&position<list.size()&&list.get(position).getSenderId()!= MyInfo.User_Id){
            return LEFT;
        }else{
            return 500;
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0+bottom_count:list.size()+bottom_count;
    }

    class SelfItem2Holder extends RecyclerView.ViewHolder{
        int position;
        RelativeLayout myself_item_2_view;
        ImageView myself_item_2_iv;
        TextView myself_item_2_tv,myself_item2_time;
        public SelfItem2Holder(View itemView) {
            super(itemView);
            myself_item_2_view= (RelativeLayout) itemView.findViewById(R.id.myself_item_2_view);
            myself_item_2_iv= (ImageView) itemView.findViewById(R.id.myself_item_2_iv);
            myself_item_2_tv= (TextView) itemView.findViewById(R.id.myself_item_2_tv);
            myself_item2_time= (TextView) itemView.findViewById(R.id.myself_item2_time);
            myself_item_2_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    return false;
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder{
        TextView bottom_tv;
        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv= (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
